package com.dsdl.chatapp.module.main;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.dsdl.chatapp.R;
import com.dsdl.chatapp.contacts.Constant;
import com.dsdl.chatapp.data.ChatList;
import com.dsdl.chatapp.data.ChatMsg;
import com.dsdl.chatapp.module.chat.ChatWindowActivity;
import com.dsdl.chatapp.module.main.adapter.ChatListAdapter;
import com.dsdl.chatapp.common.widget.ItemClickSupport;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zzg on 2016/8/3.
 * 聊天列表
 */
public class ChatListFragment extends Fragment implements ItemClickSupport.OnItemClickListener{
    public static String TAG = ChatListFragment.class.getSimpleName();
    private RecyclerView mRecyclerView;
    private ChatListReceiver mChatListReceiver;
    private ChatListAdapter mChatListAdapter;
    private List<ChatList> mChatLists = new ArrayList<>();

    public static ChatListFragment newInstance(){
        return new ChatListFragment();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mChatListReceiver = new ChatListReceiver();
        getActivity().registerReceiver(mChatListReceiver,new IntentFilter(Constant.CHAT_List_FILTER));
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_chat_list,container,false);
        initView(root);
        return root;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mChatListReceiver != null){
            getActivity().unregisterReceiver(mChatListReceiver);
        }
    }

    private void initView(View view){
        mRecyclerView = (RecyclerView) view.findViewById(R.id.rv_chat_list);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(OrientationHelper.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        ItemClickSupport.addTo(mRecyclerView).setOnItemClickListener(this);
        mChatListAdapter = new ChatListAdapter(getActivity(),mChatLists);
        mRecyclerView.setAdapter(mChatListAdapter);
    }

    @Override
    public void onItemClicked(RecyclerView recyclerView, int position, View v) {
        startActivity(new Intent(getActivity(), ChatWindowActivity.class).putExtra("phone",mChatLists.get(position).getTo()));
    }

    class ChatListReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            String messageStr = intent.getStringExtra("chat_message");
            if (!TextUtils.isEmpty(messageStr)){
                ChatMsg chatMsg = new Gson().fromJson(messageStr,ChatMsg.class);
                if (mChatLists == null){
                    mChatLists = new ArrayList<>();
                }
                if (chatMsg == null){
                    return;
                }
                String to = chatMsg.getTo();
                if (!TextUtils.isEmpty(to)){
                    ChatList chatList = getExitChatList(to,chatMsg);
                    if (chatList == null){
                        List<ChatMsg> chatMsgs = new ArrayList<>();
                        chatList = new ChatList();
                        chatMsgs.add(chatMsg);
                        chatList.setChatMsgs(chatMsgs);
                        mChatLists.add(chatList);
                    }
                    mChatListAdapter.notifyDataSetChanged();
                }

            }
        }

        /**
         * 遍历聊天列表
         * @param to
         * @param chatMsg
         * @return
         */
        private ChatList getExitChatList(String to,ChatMsg chatMsg){
            for (ChatList chatList : mChatLists){
                if (to.equals(chatList.getTo())){
                    List<ChatMsg> chatMsgs = chatList.getChatMsgs();
                    if (chatMsgs == null){
                        chatMsgs = new ArrayList<>();
                    }
                    chatMsgs.add(chatMsg);
                    chatList.setChatMsgs(chatMsgs);
                    return chatList;
                }
            }
            return null;
        }
    }
}
